<?php

namespace App\Common\Services;

use App\Common\Models\Region;
use Illuminate\Http\Request;

class Common
{
    public static function paginateDataToArray($data)
    {
        $newdata = [
            'list' => $data['data'],
            'page' => [
                'totalCount' => $data['total'],
                'currentPage' => $data['current_page'],
                'totalPage' => $data['last_page']
            ]
        ];
        return $newdata;
    }


    public static function getRegion($pid = 0)
    {
        return Region::where(['pid' => $pid])->get()->toArray();
    }


    public static function getRandomString($len = 2, $chars = null)
    {
        if (is_null($chars)) {
            $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        }
        mt_srand(10000000 * (float)microtime());
        for ($i = 0, $str = '', $lc = strlen($chars) - 1; $i < $len; ++$i) {
            $str .= $chars[mt_rand(0, $lc)];
        }
        return $str;
    }

    /**
     * 对二维数组进行指定key排序
     * @param $arr 二维数组
     * * * @param $shortKey  需要排序的列
     * * * @param $short  排序方式
     * * * @param $shortType 排序类型
       * @return mixed
     */
    public static function multiArraySort($arr, $shortKey='id', $short = SORT_DESC, $shortType = SORT_REGULAR)
    {
        foreach ($arr as $key => $data) {
            $name[$key] = $data[$shortKey];
        }
        array_multisort($name, $shortType, $short, $arr);
        return $arr;
    }

    /**
     * 一维数组找出重复的值
     * @param $array
     * @return array
     */
    public static function duplicatesArray($array)
    {
        // 统计每个值出现的次数
        $countValues = array_count_values($array);
        // 找出重复的值
        $duplicates = [];
        foreach ($countValues as $value => $count) {
            if ($count > 1) {
                $duplicates[] = $value;
            }
        }

        return $duplicates;
    }

}
